package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "套餐相关接口")
@Slf4j
public class StemealController {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @GetMapping("/admin/dish/list")
    @ApiOperation("套餐添加菜品时候根据菜品所属类别id进行查询菜品")
    public Result<List<Dish>> listByCategoryId(Long categoryId){
        List<Dish> dishes = dishService.listByCategoryId(categoryId);
        return Result.success(dishes);
    }
    @PostMapping("/admin/setmeal")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setmealService.save(setmealDTO);
        return Result.success();
    }
    @GetMapping("/admin/setmeal/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页插叙:{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
   @DeleteMapping("/admin/setmeal")
   @ApiOperation("批量输出或者单个删除")
   @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteBatch(@RequestParam  List<Long> ids){
            log.info("套餐批量删：{}",ids);
            setmealService.deleteBatch(ids);
            return Result.success();
    }
    @PostMapping("/admin/setmeal/status/{status}")
    @ApiOperation("修改状态")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStarOrStop(@PathVariable Integer status,Long id){
        setmealService.starOrStop(status,id);
        return Result.success();
    }
    @GetMapping("/admin/setmeal/{id}")
    @ApiOperation("根据套餐id回显套餐")
    public Result<SetmealVO> selectSetmealVoById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.selectWithDishById(id);
        return Result.success(setmealVO);
    }
    @PutMapping("/admin/setmeal")
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealVO setmealVO){
        setmealService.update(setmealVO);
        return Result.success();

    }





}
